﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class admin_Modules_News_Edit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
        if (!this.IsPostBack) {
            InitItem();
            LoadCategory();
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
    }
    private void LoadCategory()
    {
        using (WebData db = new WebData())
        {
            this.cbbidCat.DataSource = db.tbl_Downloads.GetDataTable("select * from tbl_TrangThai order by orderId",
                CommandType.Text);
            this.cbbidCat.DataBind();
        }

    }

    private void AddNew()
    {
        String strMess = "";
        int id = 0;
        int ID = -1;
        try
        {
            ID = Convert.ToInt32(Request.Params["ID"]);
        }
        catch { }
        try
        {
            Dami.clsFileUpLoad fileUpload = new Dami.clsFileUpLoad();
            using (WebData db = new WebData())
            {
                DataLibrary.tbl_News news = new DataLibrary.tbl_News
                { IdNew = ID, IdCat = 2, IdTrangThai = Convert.ToInt32(cbbidCat.SelectedValue), Description = txtTieude.Text, AddedDate = DateTime.Now };
                String strPath = fileUpload.FileSaveImage("upload/news", this.FileUpload1);
                if (!String.IsNullOrEmpty(strPath) && Session["image"] != null)
                {
                    String s = Session["image"].ToString();
                    fileUpload.DeleteFile(s);
                    Session.Remove("image");
                }
                else if (Session["image"] != null)
                {
                    strPath = Session["image"].ToString();
                    Session.Remove("image");
                }

                news.ImageNew = strPath;
                news.IsActive = 1;
                news.IsAuto = false;
                news.OrderId = Convert.ToInt32(txtorderId.Text);
                id = db.tbl_Newss.UpdateOnSubmit(news);
                id = 0;
                if (Session["idNewLang"] != null)
                {
                    id = Convert.ToInt32(Session["idNewLang"].ToString());
                }

                DataLibrary.tbl_News_Lang item = new DataLibrary.tbl_News_Lang();
                item.IdNewLang = id;
                item.IdLang = 1;
                item.IdNew = ID;
                item.IntroNew = txtDescription.Text;
                item.TitleNew = news.Description;
                item.BodyNew = txtNoidung.Text;
                id = db.tbl_News_Langs.UpdateOnSubmit(item);

            }
        }
        catch (Exception err)
        {
            strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
        }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
        if (id != -1)
            Response.Redirect(ViewState["URLReferrer"].ToString());

    }

    private void InitItem()
    {
        int ID = -1;
        try
        {
            ID = Convert.ToInt32(Request.Params["ID"]);
        }
        catch { }
        ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        using (WebData db = new WebData())
        {

          //  DataLibrary.tbl_Category item = db.tbl_Categorys.GetItemById(ID);

            String strSQL = "select news.idNew,IdCat,news.orderId,lang.titleNew,lang.bodyNew,lang.introNew,news.imageNew,news.isAuto"
                + ",news.addedDate,isTindacbiet,lang.idNewLang,news.IdTrangThai from tbl_News_Lang lang,tbl_News news"
                + " where lang.idNew=news.idNew and idLang=1 and isTindacbiet=0 and news.idNew=@idNew order by news.idNew desc";
            DataTable dt = db.tbl_Newss.GetDataTable(strSQL, CommandType.Text,
                new SqlParameter("@idNew", ID));

            if (dt.Rows.Count > 0)
            {
                txtDescription.Text = dt.Rows[0]["introNew"].ToString();
                txtTieude.Text = dt.Rows[0]["titleNew"].ToString();
                txtorderId.Text = dt.Rows[0]["orderId"].ToString();
                txtNoidung.Text = dt.Rows[0]["bodyNew"].ToString();
                cbbidCat.SelectedValue = dt.Rows[0]["IdTrangThai"].ToString();
                Session["image"] = dt.Rows[0]["imageNew"].ToString();
                Session["idNewLang"] = dt.Rows[0]["idNewLang"].ToString();
            }
        }
    }

}